package cn.flying.cloud.base.common.excel;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson2.JSON;

/**
 * Excel导入的监听类
 *
 * @author: admin
 * @date: 2025年04月21日 14:55
 * @version: 1.0
 */
public class ExcelReadListener<T> extends AnalysisEventListener<T> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final List<T> list;

    public ExcelReadListener(List<T> list) {
        this.list = list;
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        list.add(data);
        logger.info("解析到一条数据:{}", JSON.toJSONString(data));
        // 数据校验
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        logger.info("所有数据解析完成！");
    }


    /**
     * 返回list
     */
    public List<T> getData() {
        return this.list;
    }

}